<?php 
class GoodsController extends Controller{
	public $db;
	//构造函数
	public function __init(){
		$this->db=K('Goods');
	}
	//index方法显示商品列表
	public function index() {
		//统计总条数
		$count = $this->db->count();
		//分页处理
		$page= new Page($count,2);
		$data = $this->db->limit($page->limit())->all();
		$this->assign('goods',$data);
		$this->assign('page',$page->show());
		$this -> display();
	}
	//添加商品
	public function add(){
		if (IS_POST) {
			// p($_POST);DIE;
			if ($this->db->addGoods()) {
				$this->success('添加商品成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$gt=M('goods_type')->all();
			$category=M('category')->all();
			$this->assign('gt',$gt);
			$this->assign('category',$category);
			$this->display();
		}
	}
	//AJAX获得属性值
	public function getAttr(){
		// $gid=Q('gid','','intval');
		// p($_POST);die;
		$db=K('GoodsForm');
		//获得基本属性
		$attr=$db->get();
		$this->assign('attr',$attr);
		//获得规格属性
		$spec=$db->getSpec();
		// p($spec);die;
		$this->assign('spec',$spec);
		$this->display();
		exit;
	}
	public function edit(){
		if (IS_POST) {
			// p($_POST);DIE;
			if ($this->db->editGoods()) {
				$this->success('修改商品成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$gt=M('goods_type')->all();
			$category=M('category')->all();
			$gid=Q('gid','','intval');
			$good=M('goods')->find($gid);
			$images=M('goods_image')->where("gid=$gid")->all();
			// p($images);die;
			$this->assign('images',$images);
			$this->assign('good',$good);
			$this->assign('gt',$gt);
			$this->assign('category',$category);
			$this->display();
		}
	}
	//删除详情页图片
	public function delConImage(){
		$imageid=Q('imageid');
		$data= M('goods_image')->find($imageid);
		if(is_writable(dirname($data['big']))){
			is_file($data['big']) && @unlink($data['big']);
			is_file($data['medium']) && @unlink($data['medium']);
			is_file($data['small']) && @unlink($data['small']);
			M('goods_image')->del($imageid);
			echo 1;exit;
		}
	}
}
 ?>